﻿<div id="logOnMessage"></div>

<div id="logOnDialog" title="Log On" style="display:none;">
    <p class="validateTips"></p>
    <form action="">
	    <fieldset>
		    <label for="email">Email</label>
		    <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
		    <label for="password">Password</label>
		    <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
	    </fieldset>
	</form>
</div>
<div id="registerDialog" title="Register" style="display:none;">
    <p class="validateTips"></p>
    <form action="">
	    <fieldset>
		    <label for="email">Email</label>
		    <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
		    <label for="password">Password</label>
		    <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
	    </fieldset>
	</form>
</div>
<div id="changePasswordDialog" title="Change Password" style="display:none;">
    <p class="validateTips"></p>
    <form action="">
	    <fieldset>
		    <label for="currentPassword">Current Password</label>
		    <input type="password" name="currentPassword" id="currentPassword" value="" class="text ui-widget-content ui-corner-all" />
	        <label for="newPassword">New Password</label>
		    <input type="password" name="newPassword" id="newPassword" value="" class="text ui-widget-content ui-corner-all" />
            <label for="confirmNewPassword">Confirm New Password</label>
		    <input type="password" name="confirmNewPassword" id="confirmNewPassword" value="" class="text ui-widget-content ui-corner-all" />
        </fieldset>
	</form>
</div>

<script type="text/javascript">
    (function () {
        var formats = {
            authenticated: 'Welcome <b>{0}</b>! <span>[ <a href="javascript:;" id="logOff">Log Off</a> ] [ <a href="javascript:;" id="changePassword">Change Password</a> ]</span>',
            anonymous: '[ <a href="javascript:;" id="logOn">Log On</a> ] or [ <a href="javascript:;" id="register">Register</a> ]'
        };
        var authenticated = '@Request.IsAuthenticated' == '@bool.TrueString';
        var showAuthenticatedMessage = function (userName, autoRedirect) {
            $('#logOnMessage').html($format(formats.authenticated, userName));
            $globalVars.isAuthenticated = true;
            $globalVars.userName = userName;
            $(document.body).trigger('LogOn');
            if (autoRedirect && userName == '@SweetSurvey.Models.AccountMembershipService.AdministratorUserName') { window.location = '@Url.Action("Index", "Administrator")'; }
            var returnUrl = '@ViewBag.ReturnUrl';
            if (autoRedirect && returnUrl.length > 0 && returnUrl != 'null') { window.location = returnUrl; }
        };
        var showAnonymousMessage = function () {
            $('#logOnMessage').html(formats.anonymous);
            $globalVars.isAuthenticated = false;
            $globalVars.userName = null;
            $(document.body).trigger('LogOff');
        };
        var updateTips = function(selector, t) {
			$('.validateTips').text(t).addClass("ui-state-highlight");
		};
        var resetTips = function(selector) {
            $('.validateTips').text('All form fields are required.').removeClass("ui-state-highlight");
        };
        var resetInputs = function() {
            $('#logOnDialog input').val('');
            $('#registerDialog input').val('');
            $('#changePasswordDialog input').val('');
        };
        $(function () {
            @if (Request.IsAuthenticated) {
                @:showAuthenticatedMessage('@User.Identity.Name');
            }
            else {
                @:showAnonymousMessage();
            }

            $('#logOn').live('click', function() {
                $('#logOnDialog').dialog({
                    modal: true,
                    buttons: {
                        'Log On': function() {
                            var data = $('#logOnDialog > form').serialize();
                            $.ajax({
                                type: 'POST',
                                url: '@Url.Action("LogOnByAjax", "Account")',
                                data: data,
                                success: function(response) {
                                    if (response.indexOf('success') == 0) {
                                        $('#logOnDialog').dialog('close');
                                        showAuthenticatedMessage(response.substr(8), true);
                                    }
                                    else { updateTips('#logOnDialog .validateTips', 'Email or password is wrong.'); }
                                },
                                global: false
                            });
                        },
                        Cancel: function() { $(this).dialog('close'); }
                    }
                });
                resetTips('#logOnDialog .validateTips');
                resetInputs();
            });
            $('#logOff').live('click', function() {
                $.post('@Url.Action("LogOffByAjax", "Account")', function(response) {
                    showAnonymousMessage();
                });
            });
            $('#register').live('click', function() {
                $('#registerDialog').dialog({
                    modal: true,
                    buttons: {
                        'Create New Account': function() {
                            var data = $('#registerDialog > form').serialize();
                            $.ajax({
                                type: 'POST',
                                url: '@Url.Action("RegisterByAjax", "Account")',
                                data: data,
                                success: function(response) {
                                    if (response.indexOf('success') == 0) {
                                        $('#registerDialog').dialog('close');
                                        showAuthenticatedMessage(response.substr(8));
                                    }
                                    else { updateTips('#registerDialog .validateTips', response); }
                                },
                                global: false
                            });
                        },
                        Cancel: function() { $(this).dialog('close'); }
                    }
                });
                resetTips('#registerDialog .validateTips');
                resetInputs();
            });
            $('#changePassword').live('click', function() {
                $('#changePasswordDialog').dialog({
                    modal: true,
                    buttons:{
                        'Change Password': function() {
                            var data = $('#changePasswordDialog > form').serialize();
                            $.post('@Url.Action("ChangePasswordByAjax", "Account")', data, function(response) {
                                if (response.indexOf('success') == 0) {
                                    $('#changePasswordDialog').dialog('close');
                                    $message('Password has been successfully changed.', 'Success');
                                }
                                else { updateTips('#changePasswordDialog .validateTips', response); }
                            });
                        },
                        Cancel: function() { $(this).dialog('close'); }
                    },
                    height: 450, width: 400
                });
                resetTips('#changePasswordDialog .validateTips');
                resetInputs();
            });
            setTimeout(function() {
                if (!Modernizr.canvas) { return; }
                var register = $('#register');
                if (register.length == 0) { return; }
                register.bt(
                    'Wanna manage all your created surveys? Register now.', {
                        trigger: 'none', positions: ['bottom'], width: '210px' ,
                        cssStyles: { color: 'black', fontSize: '0.8em' },
                        textzIndex: 999, boxzIndex: 998, wrapperzIndex: 997
                    }
                );
                try { register.btOn(); } catch(err) { }
                setTimeout(function() { try { register.btOff(); } catch(err) { } }, 5 * 1000);
            }, 30 * 1000);
        });
    })();
</script>